
package Builder;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class NewMain {

    public static void main(String[] args) {
        try {
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document doc = builder.newDocument();

            //Tao doi tuong File, Content
            Node newNode = doc.createElement("account");
            Node newItem = doc.createElement("info");
            Node newName = doc.createElement("name");
            Node newPass = doc.createElement("pass");
            Node newFull = doc.createElement("full");
            Node newRole = doc.createElement("role");

            //Create nhieu Node??
            newName.setTextContent("romantic9x");
            newPass.setTextContent("123");
            newFull.setTextContent("Thao Do Danh");
            newRole.setTextContent("admin");

            newItem.appendChild(newName);
            newItem.appendChild(newPass);
            newItem.appendChild(newFull);
            newItem.appendChild(newRole);

            newNode.appendChild(newItem);

            //Item 2
            Node newItem1 = doc.createElement("info");
            Node newName1 = doc.createElement("name");
            Node newPass1 = doc.createElement("pass");
            Node newFull1 = doc.createElement("full");
            Node newRole1 = doc.createElement("role");

            newName1.setTextContent("thaodd");
            newPass1.setTextContent("1234");
            newFull1.setTextContent("Do DAnh Thao");
            newRole1.setTextContent("guest");

            newItem1.appendChild(newName1);
            newItem1.appendChild(newPass1);
            newItem1.appendChild(newFull1);
            newItem1.appendChild(newRole1);

            newNode.appendChild(newItem1);

            //Get element of file and add child
            doc.appendChild(newNode);
            Source source = new DOMSource(doc);

            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            Result result = new StreamResult(new File("taikhoan.xml"));
            System.out.println(new File("taikhoan.xml").getAbsolutePath());
            transformer.transform(source, result);
            System.out.println("success!");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
